package com.imedia.frame.lucene.service;

import java.io.File;
import java.io.IOException;

import jeasy.analysis.MMAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;

import com.imedia.frame.lucene.utils.IndexUtils;

/**
 * <p>
 * Title: 全站检索
 * </p>
 * <p>
 * Description: lucene检索模块抽象类 主要是初始化索引所需要的核心类IndexWrite</p>
 * <p>Copyright: Copyright (c) 2009 </p>
 * <p>Company: </p>
 * @author qiuy
 * @version 1.0
 */
public abstract class AbstractIndexService implements IIndexService {
	public static final String INDEX_PATH = IndexUtils.getMessage("lucene.index.path"); // 索引目录
	public static final String WHITE_FLAG = "";
	public int allCount;
	private static Analyzer standardAnalyzer; 
	private static Analyzer chineseAnalyzer; 
	private static Analyzer mmAnalyzer; 
	private File indexFile = new File(INDEX_PATH);
	protected IndexWriter writer;
	public int getAllCount() {
		return allCount;
	}
	public void setAllCount(int allCount) {
		this.allCount = allCount;
	}
	static {
		standardAnalyzer = new StandardAnalyzer();
		chineseAnalyzer = new ChineseAnalyzer();
		mmAnalyzer = new MMAnalyzer();
	}

	/**
	 * <p>
	 * Description: 初始化索引核心类用StandardAnalyzer. 参数：true为重建索引 false为追加索引
	 * </p>
	 * 
	 * @param isNew
	 */
	protected void initStandardAnalyzerIndexWrite(boolean isNew) {
		try {

			this.writer = new IndexWriter(indexFile, standardAnalyzer, isNew);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * <p>
	 * Description: 初始化索引核心类用MMAnalyzer. 参数：true为重建索引 false为追加索引
	 * </p>
	 * 
	 * @param isNew
	 */
	protected void initMMAnalyzerIndexWrite(boolean isNew) {
		try {

			this.writer = new IndexWriter(indexFile, mmAnalyzer, isNew);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * <p>
	 * Description: 初始化索引核心类用ChineseAnalyzer 参数：true为重建索引 false为追加索引
	 * </p>
	 * 
	 * @param isNew
	 */
	protected void initChineseAnalyzerIndexWrite(boolean isNew) {
		try {

			this.writer = new IndexWriter(indexFile, chineseAnalyzer, isNew);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * <p>
	 * Description: 优化索引
	 * </p>
	 * 
	 * @param objectCount
	 *            数据量大小
	 */
	protected void SEOIndexWrite(int objectCount) {

		writer.setMaxFieldLength(100000);
		writer.setMaxBufferedDocs(100);
		if (objectCount < 10000) {
			writer.setMergeFactor(50);
			writer.setMaxMergeDocs(300);
			return;
		} else if (objectCount < 50000) {
			writer.setMergeFactor(100);
			writer.setMaxMergeDocs(800);
			return;
		} else if (objectCount < 300000) {
			writer.setMergeFactor(400);
			writer.setMaxMergeDocs(2000);
		}else {
			writer.setMergeFactor(5000);
			writer.setMaxMergeDocs(100000);
		}

	}
	/**
	 * <p>
	 * Description: 初始化write
	 * </p>
	 * 
	 * @param isNew
	 *            true 重新建立索引 false 追加索引
	 */
	protected abstract void initWrite(boolean isNew);
	public void appendIndex(Object object) {
		// TODO Auto-generated method stub
		
	}
	 /* (non-Javadoc)
	 * @see cn.com.pingtech.lucene.frame.service.IIndexService#appendListIndex()
	 */
	public void appendListIndex() throws Exception {
		// TODO Auto-generated method stub
		
	}
	 public void createIndex() throws Exception {
		// TODO Auto-generated method stub
		
	}
}
